<?php
/**
 * Routes configuration
 *
 * In this file, you set up routes to your controllers and their actions.
 * Routes are very important mechanism that allows you to freely connect
 * different URLs to chosen controllers and their actions (functions).
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Config
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
/**
 * Here, we are connecting '/' (base path) to controller called 'Pages',
 * its action called 'display', and we pass a param to select the view file
 * to use (in this case, /app/View/Pages/home.ctp)...
 */
	//Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
	Router::connect('/', array('controller' => 'QLSanPhamTraCuu', 'action' => 'index'));
	Router::connect('/quan_ly_het_hang', array('controller' => 'QLThanhVienDangNhap', 'action' => 'qlhethang'));
	Router::connect('/quan_ly_admin', array('controller' => 'QLThanhVienDangNhap', 'action' => 'qladmin'));
	Router::connect('/dang_xuat', array('controller' => 'QLThanhVienDangXuat', 'action' => 'index'));
/**
 * ...and connect the rest of 'Pages' controller's URLs.
 */
	//Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
	
/**
 * xu ly duong dan trang quan tri 
 */
 	// lay url hien tai
    $url = $_SERVER['REQUEST_URI'];

	// luot bo tham so
    $url = preg_replace('/(\/[^\/:]+:[^\/:]+|\/+$|\?.+)/', '', $url);

	/**
	 * lay danh sach thanh phan trong url
	 * vd: www.abc.com/quan_tri/san_pham/tra_cuu?abc=123
	 * 		$arrUrlParts = {'quan_tri/san_pham/tra_cuu' , 'san_pham' , 'tra_cuu'} 
	 */ 
    preg_match_all('/\/quan_tri\/([^\/]+)\/([^\/^\?^:]+)/i', $url, $arrUrlParts);
	if (!empty($arrUrlParts[0])) {
		$ctrlName = 'QL';
		foreach ($arrUrlParts as $key => $value) {
			if($key != 0) {
				$wordsTemp 	= str_replace('_',' ',$value[0]);
				$wordsTemp 	= ucwords($wordsTemp);
				$ctrlName 	.= str_replace(' ','',$wordsTemp);
			}
		}		
		$router = strtolower($arrUrlParts[0][0]);
	    $routerPattern = preg_replace('/(:[^\/^:]+)/', '[\d\w]+', $router);
	    $routerPattern = preg_replace('/\//', '\\/', $routerPattern);
	    if (preg_match("/{$routerPattern}$/", $url)) {
	        $router .= '/*';
	    }
		Router::connect($router, array('action' => 'index', 'controller' => $ctrlName));
	}	

/**
 * Load all plugin routes. See the CakePlugin documentation on
 * how to customize the loading of plugin routes.
 */
	CakePlugin::routes();

/**
 * Load the CakePHP default routes. Only remove this if you do not want to use
 * the built-in default routes.
 */
	require CAKE . 'Config' . DS . 'routes.php';
